import { XLog } from '@app/x_core'
import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';


export default class EntryAbility extends UIAbility {
  onCreate(want, launchParam) {
    XLog.info('Ability onCreate');
  }

  onDestroy() {
    XLog.info('Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    XLog.info('Ability onWindowStageCreate');

    // 1.获取应用主窗口。
    let mainWindow = null;
    windowStage.getMainWindow((err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'EntryAbility', 'Failed to obtain the main window. Cause: %{public}s', JSON.stringify(err) ?? '');
        XLog.info('Ability onWindowStageDestroy');
        return;
      }
      mainWindow = data;
      hilog.info(0x0000, 'EntryAbility', 'Succeeded in obtaining the main window. Data: %{public}s', JSON.stringify(data) ?? '');
      XLog.info('Ability onWindowStageDestroy');

      // 2.实现沉浸式效果：设置导航栏、状态栏不显示。
      mainWindow.setWindowSystemBarEnable(["status"], (err) => {
        if (err.code) {
          XLog.error(`Failed to set the system bar to be visible. Cause: ${JSON.stringify(err) ?? ''}`);
          return;
        }
        XLog.info(`Succeeded in setting the system bar to be visible.`);
      })
      mainWindow.setWindowLayoutFullScreen(true)
    })

    windowStage.loadContent('pages/SplashPage', (err, data) => {
      if (err.code) {
        XLog.error(`Failed to load the content. Cause: ${JSON.stringify(err) ?? ''}`);
        return;
      }
      XLog.info(`Succeeded in loading the content. Data: ${JSON.stringify(data) ?? ''}`);
    });
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    XLog.info('Ability onWindowStageDestroy');
  }

  onForeground() {
    // Ability has brought to foreground
    XLog.info('Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    XLog.info('Ability onBackground');
  }
}
